﻿using Compass.Maui.Pages.FinishGoods;
using Compass.Maui.Services.FinishGoods;

namespace Compass.Maui.ViewModels.FinishGoods;

[QueryProperty(nameof(MainPlanDto), nameof(MainPlanDto))]
public partial class PackingInfoPageViewModel : BaseViewModel
{
    //查询有没有发货清单，有则显示入库
    //检查是否有已经入库的，显示发货
    //点击入库，进去扫码界面，下有托盘清单，扫描后弹窗选择日期，默认今天，再确定
    //更新清单中托盘状态
    private readonly IPackingListService _packingListService;
    private readonly INavigationService _navigationService;

    public PackingInfoPageViewModel(IPackingListService packingListService,INavigationService navigationService)
    {
        _packingListService = packingListService;
        _navigationService = navigationService;
        ProdRoles = "admin,prod,asm";
        WhseRoles = "admin,whse";
    }
    //控制权限

    [ObservableProperty]
    private string prodRoles;
    [ObservableProperty]
    private string whseRoles;

    [ObservableProperty]
    private MainPlanDto mainPlanDto;
    [ObservableProperty]
    private PackingListDto? packingListDto;

    [RelayCommand]
    private async Task GetPackingInfoAsync()
    {
        await IsBusyFor(async () =>
        {
            if (MainPlanDto.HavePackingList)
            {
                PackingListDto = await _packingListService.GetPackingListDtoByMainPlanIdAsync(MainPlanDto.Id.Value);
            }
            else
            {
                PackingListDto = new PackingListDto()
                {
                    HavePallet = false
                };
            }
        });
    }

    [RelayCommand]
    private async Task GoToWeighingAsync()
    {
        await IsBusyFor(async () =>
        {
            if (MainPlanDto.HavePackingList)
            {
                //传递PackingListDto
                await _navigationService.NavigateToAsync(nameof(WeighingPage),
                    new Dictionary<string, object>()
                    {
                        {nameof(PackingListDto),PackingListDto}
                    });
            }
        });
    }

    [RelayCommand]
    private async Task GoToWarehousingAsync()
    {
        await IsBusyFor(async () =>
        {
            if (MainPlanDto.HavePackingList && PackingListDto.HavePallet)
            {
                //传递PackingListDto
                await _navigationService.NavigateToAsync(nameof(WarehousingPage),
                    new Dictionary<string, object?>()
                    {
                        {nameof(PackingListDto),PackingListDto}
                    });
            }
        });
    }

    [RelayCommand]
    private async Task GoToShippingAsync()
    {
        await IsBusyFor(async () =>
        {
            if (MainPlanDto.HavePackingList && PackingListDto.HavePallet){
                //传递PackingListDto
                await _navigationService.NavigateToAsync(nameof(ShippingPage),
                new Dictionary<string, object>()
                {
                    {nameof(PackingListDto),PackingListDto}
                });
            }
        });
    }
}